package com.hefei.api.customer.manager.impl;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.hefei.api.customer.client.CustomerUrlConstants;
import com.hefei.api.customer.manager.IMenuManager;
import com.hefei.api.customer.vo.SysMenu;
import com.hefei.api.customer.vo.easyui.PageHelper;
import com.hefei.common.exception.ClientException;
import com.hefei.common.http.client.ClientFactory;
import com.hefei.common.http.request.RequestHead;
import com.hefei.common.http.response.BaseResponse;
import com.hefei.common.http.util.SendRequest;
import com.hefei.common.returncode.ReturnCode;
import com.hefei.common.util.JacksonUtil;


public class MenuManager implements IMenuManager {
	
	private static Logger logger = Logger.getLogger(MenuManager.class);
	@Override
	public Long getDatagridTotal(SysMenu menu) throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("login time:" + timer );
		RequestHead head = null;
		Long total = null;
		try {
			head = RequestHead.get(timer,ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			dataJson.putPOJO("menu", menu);
			String result = SendRequest.send(CustomerUrlConstants.SYSMENU_GET_DATAGRID_TOTAL, dataJson, timer, head);
			logger.info("login time:" + timer + " result:" + result + " cost:" + (System.currentTimeMillis() - timer));
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result, BaseResponse.class);
			if(ReturnCode.RETURN_CODE_SUCCESS.equals(baseResponse.getReturnCode())){
				String jsonStr = JacksonUtil.beanToJson(baseResponse.getReturnObject());
				Map<String, Object> map = JacksonUtil.convertToMaps(jsonStr);
				total = (Long) map.get("total");
			}
		} catch (Exception e) {
			logger.error("error", e);
			throw new ClientException(ReturnCode.RETURN_CODE_ERROR);
		}
		return total;
		
	}

	@Override
	public List<SysMenu> datagridMenu(PageHelper page) throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("time:" + timer );
		RequestHead head = null;
		try {
			head = RequestHead.get(timer,ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			dataJson.putPOJO("page", page);
			String result = SendRequest.send(CustomerUrlConstants.SYSMENU_DATA_GRID_MENU, dataJson, timer, head);
			
	 		logger.info(" time:" + timer + " result:" + result);
			logger.info( head.getLoggerStr() + " cost:" + (System.currentTimeMillis() - timer));
			
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result, BaseResponse.class);
			if(ReturnCode.RETURN_CODE_SUCCESS.equals(baseResponse.getReturnCode())){
				String listStr = JacksonUtil.beanToJson(baseResponse.getReturnObject());
				List<SysMenu> sysMenu = JacksonUtil.jsonToCollections(listStr, List.class, SysMenu.class);
				return sysMenu;
			}else{
				throw new ClientException(baseResponse.getReturnCode());
			}
		}catch(Exception e){
			logger.error("error", e);
			throw new ClientException(ReturnCode.RETURN_CODE_ERROR);
		}
	}

	@Override
	public List<SysMenu> getAll(PageHelper page) throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("time:" + timer );
		RequestHead head = null;
		try {
			head = RequestHead.get(timer,ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			dataJson.putPOJO("page", page);
			String result = SendRequest.send(CustomerUrlConstants.SYSMENU_GET_ALL_MENU, dataJson, timer, head);
			
	 		logger.info(" time:" + timer + " result:" + result);
			logger.info( head.getLoggerStr() + " cost:" + (System.currentTimeMillis() - timer));
			
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result, BaseResponse.class);
			if(ReturnCode.RETURN_CODE_SUCCESS.equals(baseResponse.getReturnCode())){
				String listStr = JacksonUtil.beanToJson(baseResponse.getReturnObject());
				List<SysMenu> sysMenu = JacksonUtil.jsonToCollections(listStr, List.class, SysMenu.class);
				return sysMenu;
			}else{
				throw new ClientException(baseResponse.getReturnCode());
			}
		}catch(Exception e){
			logger.error("error", e);
			throw new ClientException(ReturnCode.RETURN_CODE_ERROR);
		}
	}

	@Override
	public void deleteMenuById(Integer menuId) throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("time:" + timer );
		RequestHead head = null;
		try {
			head = RequestHead.get(timer,ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			dataJson.put("menuId", menuId);
			String result = SendRequest.send(CustomerUrlConstants.SYSMENU_DELETE_BY_ID_MENU, dataJson, timer, head);
			
	 		logger.info(" time:" + timer + " result:" + result);
			logger.info( head.getLoggerStr() + " cost:" + (System.currentTimeMillis() - timer));
			
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result, BaseResponse.class);
			if(!ReturnCode.RETURN_CODE_SUCCESS.equals(baseResponse.getReturnCode())){
				throw new ClientException(baseResponse.getReturnCode());
			}
		}catch(Exception e){
			logger.error("error", e);
			throw new ClientException(ReturnCode.RETURN_CODE_ERROR);
		}

	}

	@Override
	public SysMenu getMenuById(Integer menuId) throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("time:" + timer );
		RequestHead head = null;
		try {
			head = RequestHead.get(timer,ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			dataJson.put("menuId", menuId);
			String result = SendRequest.send(CustomerUrlConstants.SYSMENU_GET_MENU_BY_ID, dataJson, timer, head);
			
	 		logger.info(" time:" + timer + " result:" + result);
			logger.info( head.getLoggerStr() + " cost:" + (System.currentTimeMillis() - timer));
			
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result, BaseResponse.class);
			if(ReturnCode.RETURN_CODE_SUCCESS.equals(baseResponse.getReturnCode())){
				String ObjStr = JacksonUtil.beanToJson(baseResponse.getReturnObject());
				SysMenu sysMenu = JacksonUtil.jsonToCollections(ObjStr, SysMenu.class);
				return sysMenu;
			}else{
				throw new ClientException(baseResponse.getReturnCode());
			}
		}catch(Exception e){
			logger.error("error", e);
			throw new ClientException(ReturnCode.RETURN_CODE_ERROR);
		}
	}

	@Override
	public List<SysMenu> listAllParentMenu() throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("time:" + timer );
		RequestHead head = null;
		try {
			head = RequestHead.get(timer,ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			
			String result = SendRequest.send(CustomerUrlConstants.SYSMENU_LIST_ALL_PARENT_MENU, dataJson, timer, head);
			
	 		logger.info(" time:" + timer + " result:" + result);
			logger.info( head.getLoggerStr() + " cost:" + (System.currentTimeMillis() - timer));
			
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result, BaseResponse.class);
			if(ReturnCode.RETURN_CODE_SUCCESS.equals(baseResponse.getReturnCode())){
				String listStr = JacksonUtil.beanToJson(baseResponse.getReturnObject());
				List<SysMenu> sysMenu = JacksonUtil.jsonToCollections(listStr, List.class, SysMenu.class);
				return sysMenu;
			}else{
				throw new ClientException(baseResponse.getReturnCode());
			}
		}catch(Exception e){
			logger.error("error", e);
			throw new ClientException(ReturnCode.RETURN_CODE_ERROR);
		}
	}

	@Override
	public void saveMenu(SysMenu menu) throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("time:" + timer );
		RequestHead head = null;
		try {
			head = RequestHead.get(timer,ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			dataJson.putPOJO("menu", menu);
			String result = SendRequest.send(CustomerUrlConstants.SYSMENU_SAVE_MENU, dataJson, timer, head);
			
	 		logger.info(" time:" + timer + " result:" + result);
			logger.info( head.getLoggerStr() + " cost:" + (System.currentTimeMillis() - timer));
			
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result, BaseResponse.class);
			if(!ReturnCode.RETURN_CODE_SUCCESS.equals(baseResponse.getReturnCode())){
				throw new ClientException(baseResponse.getReturnCode());
			}
		}catch(Exception e){
			logger.error("error", e);
			throw new ClientException(ReturnCode.RETURN_CODE_ERROR);
		}

	}

	@Override
	public List<SysMenu> listSubMenuByParentid(Integer parentid) throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("time:" + timer );
		RequestHead head = null;
		try {
			head = RequestHead.get(timer,ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			dataJson.put("parentid", parentid);
			String result = SendRequest.send(CustomerUrlConstants.SYSMENU_LIST_SUB_MENU_BY_PARENTID, dataJson, timer, head);
			
	 		logger.info(" time:" + timer + " result:" + result);
			logger.info( head.getLoggerStr() + " cost:" + (System.currentTimeMillis() - timer));
			
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result, BaseResponse.class);
			if(ReturnCode.RETURN_CODE_SUCCESS.equals(baseResponse.getReturnCode())){
				String listStr = JacksonUtil.beanToJson(baseResponse.getReturnObject());
				List<SysMenu> sysMenu = JacksonUtil.jsonToCollections(listStr, List.class, SysMenu.class);
				return sysMenu;
			}else{
				throw new ClientException(baseResponse.getReturnCode());
			}
		}catch(Exception e){
			logger.error("error", e);
			throw new ClientException(ReturnCode.RETURN_CODE_ERROR);
		}
	}

	@Override
	public List<SysMenu> listAllMenu() throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("time:" + timer );
		RequestHead head = null;
		try {
			head = RequestHead.get(timer,ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			
			String result = SendRequest.send(CustomerUrlConstants.SYSMENU_LIST_ALL_MENU, dataJson, timer, head);
			
	 		logger.info(" time:" + timer + " result:" + result);
			logger.info( head.getLoggerStr() + " cost:" + (System.currentTimeMillis() - timer));
			
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result, BaseResponse.class);
			if(ReturnCode.RETURN_CODE_SUCCESS.equals(baseResponse.getReturnCode())){
				String listStr = JacksonUtil.beanToJson(baseResponse.getReturnObject());
				List<SysMenu> sysMenu = JacksonUtil.jsonToCollections(listStr, List.class, SysMenu.class);
				return sysMenu;
			}else{
				throw new ClientException(baseResponse.getReturnCode());
			}
		}catch(Exception e){
			logger.error("error", e);
			throw new ClientException(ReturnCode.RETURN_CODE_ERROR);
		}
	}

	@Override
	public List<SysMenu> listAllSubMenu() throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("time:" + timer );
		RequestHead head = null;
		try {
			head = RequestHead.get(timer,ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			
			String result = SendRequest.send(CustomerUrlConstants.SYSMENU_LIST_ALL_SUB_MENU, dataJson, timer, head);
			
	 		logger.info(" time:" + timer + " result:" + result);
			logger.info( head.getLoggerStr() + " cost:" + (System.currentTimeMillis() - timer));
			
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result, BaseResponse.class);
			if(ReturnCode.RETURN_CODE_SUCCESS.equals(baseResponse.getReturnCode())){
				String listStr = JacksonUtil.beanToJson(baseResponse.getReturnObject());
				List<SysMenu> sysMenu = JacksonUtil.jsonToCollections(listStr, List.class, SysMenu.class);
				return sysMenu;
			}else{
				throw new ClientException(baseResponse.getReturnCode());
			}
		}catch(Exception e){
			logger.error("error", e);
			throw new ClientException(ReturnCode.RETURN_CODE_ERROR);
		}
	}

	@Override
	public void insertMenu(SysMenu menu) throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("time:" + timer );
		RequestHead head = null;
		try {
			head = RequestHead.get(timer,ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			dataJson.putPOJO("menu", menu);
			String result = SendRequest.send(CustomerUrlConstants.SYSMENU_INSERT_MENU, dataJson, timer, head);
			
	 		logger.info(" time:" + timer + " result:" + result);
			logger.info( head.getLoggerStr() + " cost:" + (System.currentTimeMillis() - timer));
			
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result, BaseResponse.class);
			if(!ReturnCode.RETURN_CODE_SUCCESS.equals(baseResponse.getReturnCode())){
				throw new ClientException(baseResponse.getReturnCode());
			}
		}catch(Exception e){
			logger.error("error", e);
			throw new ClientException(ReturnCode.RETURN_CODE_ERROR);
		}

	}

	@Override
	public void updateMenu(SysMenu menu) throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("time:" + timer );
		RequestHead head = null;
		try {
			head = RequestHead.get(timer,ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			dataJson.putPOJO("menu", menu);
			String result = SendRequest.send(CustomerUrlConstants.SYSMENU_UPDATE_MENU, dataJson, timer, head);
			
	 		logger.info(" time:" + timer + " result:" + result);
			logger.info( head.getLoggerStr() + " cost:" + (System.currentTimeMillis() - timer));
			
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result, BaseResponse.class);
			if(!ReturnCode.RETURN_CODE_SUCCESS.equals(baseResponse.getReturnCode())){
				throw new ClientException(baseResponse.getReturnCode());
			}
		}catch(Exception e){
			logger.error("error", e);
			throw new ClientException(ReturnCode.RETURN_CODE_ERROR);
		}

	}

}
